/**
 * BaseAction.java
*Administrator
*2012-3-28
 */
package com.free.web.action;

import java.util.Iterator;
import java.util.Map;
import java.util.Set;

import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.interceptor.ParameterAware;

import com.free.commons.form.DynamicForm;
import com.free.constants.FrameworkConstant;

public class BaseAction implements ParameterAware {

	protected DynamicForm dynamicForm;
	
	protected void setResonse(){
		HttpServletResponse response = ServletActionContext.getResponse();
		//设置字符集编码
		response.setCharacterEncoding(FrameworkConstant.CHARCODE_UTF);
		//默认返回test/xml,处理火狐解析错误问题。
		response.setContentType(FrameworkConstant.RESPONSE_CONTENT);
	}
	
	@Override
	public void setParameters(Map<String, String[]> parameters) {
		dynamicForm = new DynamicForm();
		Set<String> keys = parameters.keySet();
		Iterator<String> itrator = keys.iterator();
		while(itrator.hasNext())
		{
			String key = itrator.next();
			String[] str = parameters.get(key);
			String value = "";
			for(int i=0,len=str.length;i<len;i++)
			{
				if(i > 0)
				{
					value += ",";
				}
				value += str[i];
			}
			if(FrameworkConstant.DATAGRID_PAGE.equals(key))
			{
				dynamicForm.setPage(Integer.parseInt(value));
			}
			else if(FrameworkConstant.DATAGRID_ROWS.equals(key))
			{
				dynamicForm.setLimit(Integer.parseInt(value));
			}
			else
			{
				dynamicForm.getMap().put(key, value);
			}
		}
	}

}
